package com.zzptc.shop.db;

import com.zzptc.shop.util.MyUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

public class MysqlDB implements IDB {
   private Connection conn;
   private Map map;
//    private String URL = "jdbc:mysql://localhost:3306/easy_shop?useUnicode=true&characterEncoding=utf-8";
//    private String UNAME = "root";
 //   private String PWD = "root";
private String DRIVER;
    private String URL;
  private String UNAME;
  private String PWD;
    @Override
    public Connection getConnection() throws SQLException {
        try {
            map=MyUtil.readXML();
            DRIVER=(String) map.get("driver");
            URL=(String) map.get("url");
            UNAME=(String) map.get("uname");
            PWD=(String) map.get("pwd");
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        conn = DriverManager.getConnection(URL, UNAME, PWD);
        return conn;
    }

    @Override
    public void closeConn() throws SQLException {

    }

    public static void main(String[] args) {
        IDB db = new MysqlDB();
        try {
            System.out.println(db.getConnection().isClosed());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
